//
//  SlidingImageView.m
//
//  Implements a sliding image screen saver view.
//
//  Lennart Lovstrand, August 1991.
//	small modifications by sam 910904
//
//  You may freely copy, distribute, and reuse the code in this example.
//  NeXT disclaims any warranty of any kind, expressed or  implied, as to its
//  fitness for any particular use.

#import "SlidingImageView.h"
#import "Thinker.h"
#import <appkit/NXImage.h>
#import <appkit/Panel.h>		// for NXRunAlertPanel()
#import <dpsclient/wraps.h>
#import <libc.h>
#import <math.h>

@implementation SlidingImageView

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	NXPoint p;
	if (!rects || !rectCount) return self;

	[super drawSelf:rects :rectCount];
	
	p.x = floor(imageRect.origin.x);
	p.y = floor(imageRect.origin.y);

    [image composite: NX_SOVER toPoint: &p];

	return self;
}

- oneStep
{
	NXPoint p;
    PSsetgray(0);
    NXRectFill(&imageRect);
    
	usleep((1*1000000)/68);					//sleep a few vblanks
    imageRect.origin.x += delta.x;
    imageRect.origin.y += delta.y;

    if (imageRect.origin.x < 0)
	{
		imageRect.origin.x = 0;
		delta.x = randBetween(.8,1.3);
    }
	else if (imageRect.origin.x > maxCoord.x)
	{
		imageRect.origin.x = maxCoord.x;
		delta.x = -randBetween(.8,1.3);
    }

    if (imageRect.origin.y < 0)
	{
		imageRect.origin.y = 0;
		delta.y = randBetween(.8,1.3);
    }
	else if (imageRect.origin.y > maxCoord.y)
	{
		imageRect.origin.y = maxCoord.y;
		delta.y = -randBetween(.8,1.3);
    }

	p.x = floor(imageRect.origin.x);
	p.y = floor(imageRect.origin.y);

    [image composite: NX_SOVER toPoint: &p];

    return self;
}

- setImageConstraints
{
    [super setImageConstraints];

	if (imageRect.origin.x > maxCoord.x ||
		imageRect.origin.y > maxCoord.y)
	{
		imageRect.origin.x = randBetween(0, maxCoord.x);
		imageRect.origin.y = randBetween(0, maxCoord.y);
	}

	delta.x = randBetween(-1,1);
	delta.y = randBetween(-1,1);

    return self;
}

- inspector:sender
{
	return [sender commonImageInspector];
}

@end
